package com.shiloop.flashlight;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import java.util.ArrayList;

public class FlashlightActivity extends Activity implements
        FlashFrameLayout.OnViewChangeListener, LockImageButton.OnLockChangeListener {
    private static final String TAG = "FlashlightActivity";
    private Flashlight flashlight;
    private boolean mIsLocked;
    private LockImageButton lockButton;
    private FlashFrameLayout viewContainer;
    private ArrayList<AbstractFlashView> flashViews;
    private SoundPool mSoundPool;
    private int mCurrentIndex; // which abstract flash view is showed
    private int mLockSoundID;
    private int mUnlockSoundID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);
        lockButton = (LockImageButton) findViewById(R.id.swap_lock_button);
        viewContainer = (FlashFrameLayout) findViewById(R.id.view_container);

        // initial status
        mIsLocked = false;
        lockButton.setSoundEffectsEnabled(true);
        // Sound pool
        mSoundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
        mLockSoundID = mSoundPool.load(this, R.raw.lock, 1);
        mUnlockSoundID = mSoundPool.load(this, R.raw.unlock, 1);

        // flash light
        flashlight = new Flashlight();

        // init abstract views
        flashViews = new ArrayList<AbstractFlashView>();
        initAbstractViews();
        if (flashViews.size() >= 1) {
            mCurrentIndex = 0;
            flashlight.setFlashView(flashViews.get(mCurrentIndex));
        }
        // set listener
        viewContainer.setOnViewChangeListener(this);
        lockButton.setOnLockChangeListener(this);
        lockButton.setLock(mIsLocked);
        lockButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lockButton.setLock(!lockButton.isLocked());
            }
        });
    }

    private void initAbstractViews() {
        final int childCount = viewContainer.getChildCount();
        int i = 0;
        while (i < childCount) {
            AbstractFlashView view = (AbstractFlashView) viewContainer.getChildAt(i);
            flashlight.addFlashlightListener(view);
            flashViews.add(view);
            i++;
        }
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
        flashlight.release();
        super.onDestroy();
    }

    @Override
    public void onLockChange() {
        mIsLocked = lockButton.isLocked();
        viewContainer.setLock(mIsLocked);
        updateLockButtonStatus();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        updateLockButtonStatus();
    }

    private void updateLockButtonStatus() {
        Log.d(TAG, "update lock button");
        if (mIsLocked) {
            lockButton.setImageResource(android.R.drawable.ic_secure);
            mSoundPool.play(mLockSoundID, 1, 1, 0, 0, 1);
        } else {
            lockButton.setImageResource(android.R.drawable.ic_partial_secure);
            mSoundPool.play(mUnlockSoundID, 1, 1, 0, 0, 1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.flashlight, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    @Override
    public void onViewChange(int view) {
        if (!flashViews.isEmpty()) {
            flashlight.setFlashView(flashViews.get(view));
        }
    }

}
